package com.kgc.community.controller;

import com.kgc.community.mapper.RoomUserMapper;
import com.kgc.community.pojo.RoomUser;
import com.kgc.community.pojo.User;
import com.kgc.community.service.RoomUserAddService;
import com.kgc.community.service.UserAddService;
import com.kgc.community.utils.ExcelUtil;
import com.kgc.community.utils.MyMD5;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @Author Mr.Man
 * @Date 2020-12-06 15:29
 **/
@Controller
public class MoreUserController {

    @Resource
    UserAddService userAddService;

    @Resource
    RoomUserAddService roomUserAddService;

    /**
     * 进入批量添加页面
     * @return
     */
    @RequestMapping("moreuser.do")
    public String moreuser(){
        return "morebuildadd";
    }

    @RequestMapping("addUsermore.do")
    public String addUsermore(@RequestParam("file") CommonsMultipartFile file,Integer typeuser ,Model model) throws IOException {
        String path="E:/"+new Date().getTime()+file.getOriginalFilename();
        File newFile=new File(path);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        file.transferTo(newFile);
        ExcelUtil excelUtil = new ExcelUtil();
        List<List<String>> list = excelUtil.readWork(newFile);

        if(typeuser == 7){
            for (int i = 1; i < list.size(); i++) {
                List<String> strings = list.get(i);
                User user = new User();
                user.setId(Integer.parseInt(strings.get(3)));
                user.setTel(strings.get(3));
                user.setCardId(strings.get(4));
                user.setPwd(MyMD5.MD5(strings.get(4).substring(strings.get(4).length()-4,strings.get(4).length())));
                user.setName(strings.get(1));
                user.setSex(Integer.parseInt(strings.get(2)));
                user.setRoleId(typeuser);
                user.setStatus(1);
                Integer integer = userAddService.addUser(user);

                RoomUser roomUser = new RoomUser();

                roomUser.setRoomId(strings.get(0));
                roomUser.setUserId((Integer.parseInt(strings.get(3))));
                roomUser.setCreateTime(new Date());
                roomUser.setStatus(0);
                roomUser.setPicture("");
                roomUserAddService.addRoomUser(roomUser);

            }
        }else{
            //管理者

            for (int i = 1; i < list.size(); i++) {
                List<String> strings = list.get(i);
                User user = new User();
                user.setId(Integer.parseInt(strings.get(3)));
                user.setTel(strings.get(3));
                user.setCardId(strings.get(4));
                user.setPwd(MyMD5.MD5(strings.get(4).substring(strings.get(4).length()-4,strings.get(4).length())));
                user.setName(strings.get(1));
                user.setSex(Integer.parseInt(strings.get(2)));
                user.setRoleId(typeuser);
                user.setStatus(1);
                Integer integer = userAddService.addUser(user);
            }

        }
        model.addAttribute("msg","上传成功");
        return "morebuildadd";
    }
}
